package com.shigui.news.controller.api;



import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.shigui.news.controller.BaseAction;
import com.shigui.news.enums.ResponseCode;
import com.shigui.news.exception.CategoryException;
import com.shigui.news.interceptor.JsonFormat;
import com.shigui.news.model.Category;
import com.shigui.news.model.News;
import com.shigui.news.model.User;
import com.shigui.news.service.CategoryService;
import com.shigui.news.service.NewsService;
@Scope("prototype")
@Controller("categoryAction")
public class CategoryAction extends BaseAction {

	@Autowired
	private CategoryService categoryService;

	
	private Category category;
	
	public void add(){
		
		User userLogin = findByPersistCodeOrFail();
		
		if(userLogin == null){
			
			throw new CategoryException(ResponseCode.CATEGORY_ADD);
		}
		
		categoryService.insert(category);
		
		getHttpRequest().setAttribute(JsonFormat.RESPONSE_MESSAGE_KEY, category.getCateName());
		getHttpRequest().setAttribute(JsonFormat.RESPONSE_DATE_KEY, category);
		
	}
	/**
	 * 删除类别
	 */
	public void delete(){
		
		User userLogin = findByPersistCodeOrFail();
		
		Category cate = categoryService.selectById(category.getCateId());
		
//		getHttpRequest().setAttribute(JsonFormat.RESPONSE_DATE_KEY, cate);
		
		categoryService.delete(userLogin,cate);
		
	}
	
	public void select(){
		
		Category cate = categoryService.selectById(category.getCateId());
		
		getHttpRequest().setAttribute(JsonFormat.RESPONSE_MESSAGE_KEY, cate.getCateName());
		getHttpRequest().setAttribute(JsonFormat.RESPONSE_DATE_KEY, cate);
	}
	
	public void findCategory(){
		
		User userLogin = findByPersistCodeOrFail();
		
		if(userLogin == null){
			
			throw new CategoryException(ResponseCode.CATEGORY_ADD);
		}
		try{
			categoryService.select(category.getCateId(),  "from Category where cateId=:cateId");
			
		}catch (Exception e) {
			
			throw new CategoryException(ResponseCode.CATEGORY_FIND_NEWS);
		}
		
		getHttpRequest().setAttribute(JsonFormat.RESPONSE_MESSAGE_KEY, category.getCateName());
		getHttpRequest().setAttribute(JsonFormat.RESPONSE_DATE_KEY, category);
		
		
	}
	/**
	 * 分页
	 */
	
	public void categoryList(){
		
		
		
		List<Category> categories = categoryService.select();
		/**
		 * 格式化返回结果
		 */
		List<Category> cateList = new ArrayList<>();
		for(Category category : categories){
			Category cat = new Category();
			
			cat.setCateName(category.getCateName());
			cateList.add(cat);
			
		}
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("page", getPage());
		data.put("count", getCount());
		data.put("totalPage", getTotal());
		data.put("data", cateList);
		
		getHttpRequest().setAttribute(JsonFormat.RESPONSE_DATE_KEY, data);
		
		
	}
	public void update(){
		
		User userLogin = findByPersistCodeOrFail();

		
		categoryService.update(userLogin,category);
		
		getHttpRequest().setAttribute(JsonFormat.RESPONSE_DATE_KEY, category);
	}
	
	
	
	
	public Category getCategory() {
		return category;
	}
	public void setCategory(Category category) {
		this.category = category;
	}
	
	
	
	
}
